#include<bits/stdc++.h>

using namespace std;
int m,n,h;
const int N=5e4+10;
int s[N],a[N];
unordered_map<int,int>lqq,lqq1;
typedef pair<int,int>PLL;
vector<PLL>l;
int main(){
	//freopen("C:\\Users\\29842\\Desktop\\in.in","r",stdin);
	cin>>m>>n;
    for(int i=0;i<m;i++)scanf("%d",&s[i]),lqq[s[i]]++;//cout<<s[i]<<endl;
    while(n--){
		int a1,b;
		cin>>a1>>b;
		for(auto &i:lqq){
			int b1=i.first%10,c=i.first/100,d=i.first/10%10;
//	cout<<b1<<" "<<c<<" "<<d<<endl;
			if(b1==a1||b1==b||c==b||c==a1||d==a1||d==b){
			   lqq1[i.first]+=lqq[i.first];
			}
		}
	}
	for(auto &j:lqq1){
		//cout<<j.second<<endl;
		l.push_back({-j.second,j.first});
	}
	sort(l.begin(),l.end());
	for(int i=0;i<3;i++)
	{
		printf("%03d ",l[i].second);
	}
}
